/**
 * 参数处理
 * @param {*} params  参数
 */
export const tansParams = (params: Record<string, unknown>): string => {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    const part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object" && !Array.isArray(value)) {
        for (const key of Object.keys(value as Record<string, unknown>)) {
          const subValue = (value as Record<string, unknown>)[key];
          if (
            subValue !== null &&
            subValue !== "" &&
            typeof subValue !== "undefined"
          ) {
            const subKey = `${propName}[${key}]`;
            const subPart = encodeURIComponent(subKey) + "=";
            result += subPart + encodeURIComponent(String(subValue)) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(String(value)) + "&";
      }
    }
  }
  return result;
};

export const formatTimestamp = (timestamp: string | number): string => {
  // 转成数字
  let ts = Number(timestamp);

  // 如果是秒级（10位），转换成毫秒
  if (ts.toString().length === 10) {
    ts = ts * 1000;
  }

  const date = new Date(ts);

  const Y = date.getFullYear();
  const M = String(date.getMonth() + 1).padStart(2, "0");
  const D = String(date.getDate()).padStart(2, "0");
  const h = String(date.getHours()).padStart(2, "0");
  const m = String(date.getMinutes()).padStart(2, "0");
  const s = String(date.getSeconds()).padStart(2, "0");

  return `${Y}-${M}-${D} ${h}:${m}:${s}`;
};
